import {Component, Inject, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {UtilityService} from '../../../../../../service/utils.service';
import {appConfig} from '../../../../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';
import * as moment from 'moment';

@Component({
  selector: 'app-profilsWork',
  templateUrl: './profilsWork.component.html',
})
export class  ProfilsWorkComponent implements OnInit {

    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        public activatedRoute: ActivatedRoute,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    ) { }

    profilesGuid: string; // 工作项guid
    data = []; // 数据
    isPagination = true; // 是否有分页
    pageIndex = 1; // 当前页数
    total: number;
    pageTotal: number;
    isShowTotal: boolean;  // 是否显示总数
    showAdd: boolean;  // 是否显示按钮和新增
    page: any; // 翻页
    token:any;
    // 工作项guid
    headerDate = [  // 配置表头内容
        { label: '工作项名称', value: 'itemName', isclick: false},
         { label: '工作项类型', value: 'workType', isclick: false},
          { label: '状态', value: 'itemStatus', isclick: false},
           { label: '启动时间', value: 'developStartTime', isclick: false},
          { label: '负责人', value: 'owner', isclick: false},
    ];

    isData = true;
branchInfo = false;

    ngOnInit() {
        this.token  = this.tokenService.get().seniortoken; // 绑定token
        this.profilesGuid = this.activatedRoute.snapshot.params.id; // 拿到父组件传过来的操作员的guid来进行操作
        this.isShowTotal = true;
        this.showAdd = true;
        this.getData();
    }
pageindex=1;
checkModalData = [];
dataTotal:number;
searchwork:string;
    getData(option?) {
          if (option) {
           this.pageindex =1;
        }
       
        // return;
        const page = {
                    condition: {
                        itemName:this.searchwork
                     }, // 搜索内容
                        page: {
                            size: 10,
                            current: this.pageindex
                        }
                    };
        let url='/sProfiles/'+this.profilesGuid+'/PutWorkitemDetail'
        this.listloading = true;
        this.utilityService.postData(appConfig.testUrl  + url,page)
            .subscribe(
                (val) => {
            this.listloading = false;
            this.data = val.result.records;
          
              this.dataTotal = val.result.pages * 10;  
            this.data.forEach(i=>{
                 i.buttonData = [
                                    {key: 'look', value: '查看代码'},
                                    {key: 'delay', value: '延期申请'},
                                ];
                i.developStartTime = moment(i.developStartTime).format('YYYY-MM-DD');

          })
        

        },   (error) => {
             this.listloading = false;
                    this.nznot.create('error', error.code , error.msg);
                })

    }
    searchRevert:any;
    delayListModal = false;
    loading = false;
    isdelayPagination = true;
    delayListData = [];
    delayPageIndex = 1;
    workId:number;
    delayHeaderDate = [
        { label: '序号', value: 'index', isclick: false },
        { label: '代码', value: 'fullPath', isclick: false }
    ]
    listloading = false;
       isCaocuo = false;
    delaypageTotal:number;
        buttonEvent(it) {
            this.workId = it.guid;
            if(it.names.key === "look"){
                 let url='/sProfiles/'+this.profilesGuid+'/workitem/'+ this.workId;
                    let index ='';
                    let star = '';
                    let end = '';
                    this.utilityService.getData(appConfig.testUrl  + url)
                        .subscribe(
                            (val) => {
                                this.branchInfo =true;
                        this.checkModalData = val.result
                        for  (let i = 0; i < this.checkModalData.length; i ++) {
                                            for (let x = 0; x < this.checkModalData[i].deliveryPatchDetails.length; x ++) {
                                                for (let  y = 0; y < this.checkModalData[i].deliveryPatchDetails[x].fileList.length; y ++) {
                                                    this.checkModalData[i].deliveryPatchDetails[x].fileList[y]['buttons'] = null
                                                    if (this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath) {
                                                        index = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.indexOf(this.checkModalData[i].projectName);
                                                        this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substring(index, this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length);
                                                        if(this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length > 60) {
                                                                    star = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substr(0, 30)
                                                                    end = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substr(this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length - 30)
                                                                        this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPathstr = star + '...' + end;
                                                                    }else{
                                                                    this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPathstr = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath;
                                                                }

                                        }
                                                }
                                            }
                                        }
                    },   (error) => {

                                this.nznot.create('error', error.code , error.msg);
                            })
            }else  if(it.names.key === "delay"){
              
                
                this.getdelay()
      
            }
       
        }
        isInArray(arr,value){
    for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return true;
        }
    }
    return false;
}
    delaysSeclect = []
        getdelay(option?){
          if (option) {
           this.delayPageIndex =1;
        }
        const page = {
                    condition: {fullPath:this.searchRevert}, // 搜索内容
                        page: {
                            size: 10,
                            current: this.delayPageIndex
                        }
                    };
                      this.listloading = true;
            this.utilityService.postData(appConfig.testUrl  +'/sWorkitem/'+ this.workId+'/standardListAll ', page )
                .subscribe(
                    (val) => {
                    this.delayListData = val.result.records
                                  this.listloading = false;
                                  val.result.records.forEach((i,index)=>{
                                      val.result.records[index]['index'] = index+1;
                                      i.checked = false;
                                      this.delaysSeclect.forEach(j=>{
                                          if(i.guid === j){
                                              i.checked = true;
                                          }
                                      })
                                  });
                                     
                                  this.delaypageTotal = val.result.pages * 10;  
                                  this.delayListModal = true
                    },(error)=>{
                        this.delayListModal = false;
        this.nznot.create('error',error.code ,error.msg );
                    })
         }
         delList(){
             this.reason = null;
            this.getdelays = [];
            this.delaysSeclect = []
             this.delayListModal=false
         }
        getGodelay(){
            if(!this.reason){
                   this.nznot.create('error','请填写撤销理由' ,'');
                   return;
            }
                let obj = {
                    guidProfiles: this.profilesGuid,
                    guidStandarLists: this.delaysSeclect,
                    guidWorkitem: this.workId,
                    repealDesc: this.reason
                }
                this.listloading = true;
               this.utilityService.postData(appConfig.testUrl  +'/sRepeal', obj )
                .subscribe(
                    (val) => {
                this.listloading = false
                this.delayListModal = false;
                this.rllbackModal = false;
                this.nznot.create('success', val.code , val.msg);
                    },(error)=>{
                    this.listloading = false
                    this.rllbackModal = false;
                    this.nznot.create('error', error.code , error.msg);
                    })
        }
         rllbackModal = false;
         reason:string;
        getGodelayList(){
          
            if(this.delaysSeclect.length === 0 && this.getdelays.length === 0){
              this.nznot.create('error','请勾选代码' ,'');
              return
          }
       this.reason = null;
          this.rllbackModal = true;
        }
        // 选中复选框方法
    getdelays = []
 
    selectId:number;
    selectedRow(event) {
       
      
            this.getdelays = []
        event.selectedRows.forEach(i=>{
               this.getdelays.push(i.guid)     
        })

          let obj = []
         
             this.getdelays.forEach(i=>{
                let str =  this.isInArray(this.delaysSeclect,i);
                if(!str){
                 this.delaysSeclect.push(i) 
                }
             })
               if(event.it && !event.it.checked){
            this.selectId = event.it.guid
               this.delaysSeclect.forEach(k=>{
                if(k!==this.selectId){
                obj.push(k)
                }
            })
              this.delaysSeclect = obj;
        }
         
    }

 
       onSearch(it){
         this.searchRevert = it;
         this.getdelay(it)
      console.log(it)
     } 
        delayMonitorHandler(event){
        this.delayPageIndex = event;
        this.getdelay();
    }
    pageindexHandler(event){
    this.pageindex = event;
    this.getData()
    }
    isActive(it){
        console.log(it)

        //  this.router.navigate(['workView'],
        //     { queryParams:
        //             { workGuid: it.guid
        //             }
        //     });
    }
    savemergeisInfo(){
        
    }




}
